//
//  ViewController.m
//  imageSelected
//
//  Created by admin on 17/9/25.
//  Copyright © 2017年 鲍志刚. All rights reserved.
//

#import "ViewController.h"
#import "CollectionViewCell.h"
#import "KNTakePhoto.h"

#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *selectedArray;

@property (nonatomic, strong) UIImage *image;

@end

@implementation ViewController

-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
        flowLayOut.itemSize = CGSizeMake((SCREEN_WIDTH - 60)/ 2, (SCREEN_WIDTH - 60)/ 2);
        //flowLayOut.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 80, SCREEN_WIDTH - 40, 550) collectionViewLayout:flowLayOut];
        _collectionView.backgroundColor = [UIColor cyanColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

-(NSMutableArray *)selectedArray {
    if (_selectedArray == nil) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //[self.selectedArray addObject:[UIImage imageNamed:@"login_home"]];
    
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.selectedArray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row == self.selectedArray.count) {
        cell.imagev.image = [UIImage imageNamed:@"AlbumAddBtn"];
        //cell.imagev.backgroundColor = [UIColor redColor];
        cell.deleteButton.hidden = YES;
    } else {
        cell.imagev.image = self.selectedArray[indexPath.item];
        cell.deleteButton.hidden = YES;
    }
    [cell layoutIfNeeded];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(@"第%ld组 第%ld行",(long)indexPath.section,(long)indexPath.item);
    if (indexPath.row == self.selectedArray.count) {
        //[self.selectedArray addObject:[UIImage imageNamed:@"login_home"]];
        
        [KNTakePhoto shareViewController:self Picture:^(UIImage *HeadImage){
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.image = HeadImage;
                [self.selectedArray addObject:self.image];
                
                CollectionViewCell * cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
                cell.imagev.image = HeadImage;
                [self.collectionView reloadData];
                NSLog(@"数组包含图片%lu",(unsigned long)self.selectedArray.count);
                
                [self scrollToTableBottom];
               
            });
        }];

        //[self.collectionView reloadData];

    } else {
        //[self addPictureClick];
        NSLog(@"数组包含图片%lu",(unsigned long)self.selectedArray.count);
        //[self.collectionView reloadData];
    }
}


- (void)scrollToTableBottom {
    NSInteger lastRow = self.selectedArray.count;
    if (lastRow < 0) return;
    
    NSIndexPath *lastPath  = [NSIndexPath indexPathForItem:lastRow inSection:0];
    [self.collectionView scrollToItemAtIndexPath:lastPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
